package service

import (
	"reflect"
)

type service interface {
	Name() string
}

type serviceInfo struct {
	methods map[string]*Method
}

func newServiceInfo() *serviceInfo {
	result := new(serviceInfo)
	result.methods = make(map[string]*Method)

	return result
}

func (info *serviceInfo) register(methodName string, methodType reflect.Method, methodValue reflect.Value, handler func(param reflect.Type)) {
	for _, name := range []string{
		"Name",
	} {
		if name == methodName {
			return
		}
	}

	// parse parameters
	paramType := methodType.Type
	paramNum := paramType.NumIn()
	paramCount := 0
	for i := 1; i < paramNum; i++ {
		inType := paramType.In(i)
		if handler != nil {
			handler(inType)
		}

		paramCount++
	}

	method := newMethod(
		methodType,
		methodValue,
		paramCount,
	)

	info.methods[methodName] = method
}

func (info *serviceInfo) get(name string) (result *Method, exists bool) {
	result, exists = info.methods[name]
	return
}
